package entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.Size;

@Entity
@NamedQueries({
    // Wordt gebruikt door Rein, Ies
    @NamedQuery(
        name = "allCategories",
        query = "SELECT c FROM Category c ORDER BY c.name ASC"
    ),
    //Ies
    @NamedQuery(
        name = "getCategoryById",
        query = "SELECT c FROM Category c WHERE c.id = :categoryId"
    ),
    // Deze query is nu nog bijna hetzelfde als "allCategories" 
    // maar wordt denk ik nog uitgebreidt met pagination
    @NamedQuery(
        name = "categoryAdminList",
        query = "SELECT c FROM Category c"
    )
})
public class Category implements Serializable {
    
    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;
    private List<Product> products;
    private Image image;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Size(min = 1, max = 70, message = "Name invalid (1-70 characters")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    @OneToMany(mappedBy = "category")
    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }
    
    @OneToOne
    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Category)) {
            return false;
        }
        Category other = (Category) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Category[ id=" + id + " ]";
    }

}
